package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.rank.UnionRank;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.logic.cross.CrossGroupUnionRankListModel;
import com.yanqu.road.logic.pb.CrossUnionPb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.rank.CrossRankMgr;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.union.CrossUnionRankMgr;
import com.yanqu.road.server.manager.union.unionrank.CrossUnionRankMsgCacheMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = CrossProtocol.C_CROSS_SERVER_GROUP_UNION_RANK,desc = "分组商会榜单")
public class GetCrossServerGroupUnionRankCmd extends CrossCommand {
    private long userId;
    private int groupId;
    private String unionId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RankProto.CrossGroupUnionRankReqMsg req = RankProto.CrossGroupUnionRankReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        groupId = req.getGroupId();
        unionId = req.getUnionId();
        if(0 == groupId){
            return false;
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
//        CrossGroupUnionRankListModel rankListModel = CrossUnionRankMgr.getAllCrossGroupUnionRankListModelMap().get(groupId);
//        if (null == rankListModel) {
//            log.info("GroupUnionRankList error,serverId:{},userId:{},groupId:{}", serverId, userId, groupId);
//            return;
//        }
//
//        int myRank = -1;
//
//        RankProto.UnionRankListMsg.Builder unionListMsg = RankProto.UnionRankListMsg.newBuilder();
//        List<UnionRank> rankList = new ArrayList<>(rankListModel.getRankList());
//        int rank = 1;
//        for (UnionRank unionRank : rankList) {
//            RankProto.UnionRankTempMsg.Builder rankTemp = RankProto.UnionRankTempMsg.newBuilder();
//            rankTemp.setUnionUid(unionRank.getUnionUid());
//            UnionProto.UnionBaseTempMsg.Builder unionMsg = UnionBasePb.parseUnionBaseTempMsg(unionRank.getUnionBaseInfo());
//            rankTemp.setUnionBaseData(unionMsg);
//            CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionRank.getUnionUid());
//            if (null != unionInfo) {
//                List<UnionProto.UnionMemberRankGradeTemp> memberRankGradeMsg = CrossUnionMgr.getMemberRankGradeMsg(unionInfo);
//                rankTemp.addAllRankGrade(memberRankGradeMsg);
//
//                for (Map.Entry<Integer, Integer> gradeEntry : unionInfo.getGradeAmountMap().entrySet()) {
//                    rankTemp.addOriginalGradeAmount(CrossUnionPb.parseUnionRankGradeJoinLimitTemp(gradeEntry.getKey(), gradeEntry.getValue()));
//                }
//            }
//
//            unionListMsg.addRankList(rankTemp);
//
//            if (unionRank.getUnionUid().equals(unionId)) {
//                myRank = rank;
//            }
//            rank++;
//        }
////        RankProto.UnionRankListMsg.Builder unionListMsg = rankListModel.getRankListMsg();
//        RankProto.UnionRankRespMsg.Builder respMsg = RankProto.UnionRankRespMsg.newBuilder();
//        respMsg.setRankList(unionListMsg);
////        int myRank = rankListModel.getMyRank(unionId);
//        respMsg.setMyRank(myRank);
//        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_UNION_RANK_LIST, respMsg);
//        MessageHelper.sendPacket(serverId, userId, message);


        RankProto.UnionRankListMsg unionCrossRankMsg = CrossUnionRankMsgCacheMgr.getUnionCrossRankMsg(serverId);
        if (unionCrossRankMsg == null) {
            log.info("GroupUnionRankList error,serverId:{},userId:{},groupId:{}", serverId, userId, groupId);
            return;
        }

        int myRank = -1;

        for (int i = 0; i < unionCrossRankMsg.getRankListList().size(); i++) {
            int rank = i + 1;
            RankProto.UnionRankTempMsg rankTemp = unionCrossRankMsg.getRankListList().get(i);
            if (rankTemp.getUnionUid().equals(unionId)) {
                myRank = rank;
                break;
            }
        }
        RankProto.UnionRankRespMsg.Builder respMsg = RankProto.UnionRankRespMsg.newBuilder();
        respMsg.setRankList(unionCrossRankMsg);
        respMsg.setMyRank(myRank);
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_UNION_RANK_LIST, respMsg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
